import {createRouter, createWebHashHistory, RouteRecordRaw} from "vue-router";
import Home from "../views/home.vue";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

const routes: RouteRecordRaw[] = [
    {
        path: "/",
        redirect: "/content",
    },
    {
        path: "/schoolAdmin",
        name: "schoolAdmin",
        redirect: "/schoolAdmin/contentAdmin",
        component: Home,
        meta: {
            title: "学校后台",
        },
        children: [
            {
                path: "contentAdmin",
                name: "contentAdmin",
                meta: {
                    title: "内容管理",
                    permiss: "contentAdmin"
                },
                component: () => import("../views/schoolAdmin/content.vue")
            },
            {
                path: "questionAdmin",
                name: "questionAdmin",
                meta: {
                    title: "答疑管理",
                    permiss: "questionAdmin"
                },
                component: () => import("../views/schoolAdmin/question.vue")
            },
            {
                path: "infoAdmin",
                name: "infoAdmin",
                meta: {
                    title: "信息维护",
                    permiss: "infoAdmin"
                },
                component: () => import("../views/schoolAdmin/info.vue")
            },
            {
                path: "knowledgeAdmin",
                name: "knowledgeAdmin",
                meta: {
                    title: "知识库",
                    permiss: "knowledgeAdmin"
                },
                component: () => import("../views/schoolAdmin/knowledge.vue")
            },
        ]
    },
    {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/content",
                name: "content",
                meta: {
                    title: "内容管理",
                    permiss: "content"
                },
                component: () => import("../views/content/index.vue")
            },
            {
                path: "/advertisement",
                name: "advertisement",
                meta: {
                    title: "广告管理",
                    permiss: "advertisement"
                },
                component: () => import("../views/advertisement/index.vue")
            },
            {
                path: "/question",
                name: "question",
                meta: {
                    title: "答疑管理",
                    permiss: "question"
                },
                component: () => import("../views/question/index.vue")
            },
            {
                path: "/roles",
                name: "roles",
                meta: {
                    title: "角色管理",
                    permiss: "roles"
                },
                component: () => import("../views/roles/index.vue")
            },
            {
                path: "/reviews",
                name: "reviews",
                meta: {
                    title: "审核管理",
                    permiss: "reviews"
                },
                component: () => import("../views/reviews/index.vue")
            },
            {
                path: "/schools",
                name: "schools",
                meta: {
                    title: "学校管理",
                    permiss: "schools"
                },
                component: () => import("../views/schools/index.vue")
            },
            {
                path: "/visualization",
                name: "visualization",
                meta: {
                    title: "可视化管理",
                    permiss: "visualization"
                },
                component: () => import("../views/visualization/index.vue")
            },
            {
                path: "/aiQA",
                name: "aiQA",
                meta: {
                    title: "智能问答",
                    permiss: "aiQA"
                },
                component: () => import("../views/aiQA/index.vue")
            },
            {
                path: "/addContent",
                name: "addContent",
                meta: {
                    title: "新增文章",
                    permiss: "addContent",
                    hidden: true
                },
                component: () => import("../views/common/addContent.vue")
            },
            {
                path: "/addContentSchool",
                name: "addContentSchool",
                meta: {
                    title: "新增文章",
                    permiss: "addContentSchool",
                    hidden: true
                },
                component: () => import("../views/common/addContentSchool.vue")
            },
            {
                path: "/classifiedStatistics",
                name: "classifiedStatistics",
                meta: {
                    title: "分类统计",
                    permiss: "classifiedStatistics",
                    hidden: true
                },
                component: () => import("../views/common/classifiedStatistics.vue")
            },
            {
                path: "/articleDetails",
                name: "articleDetails",
                meta: {
                    title: "文章详情",
                    permiss: "articleDetails",
                    hidden: true
                },
                component: () => import("../views/common/articleDetails.vue")
            },
            {
                path: "/schoolInfo",
                name: "schoolInfo",
                meta: {
                    title: "学校信息",
                    permiss: "schoolInfo",
                    hidden: true
                },
                component: () => import("../views/common/schoolInfo.vue")
            },
            {
                path: "/addVisualization",
                name: "addVisualization",
                meta: {
                    title: "新增可视化",
                    permiss: "addVisualization",
                    hidden: true
                },
                component: () => import("../views/common/addVisualization.vue")
            },
            {
                path: "/knowledgeDetail",
                name: "knowledgeDetail",
                meta: {
                    title: "知识库详情",
                    permiss: "knowledgeDetail",
                    hidden: true
                },
                component: () => import("../views/common/knowledgeDetail.vue")
            },
            {
                path: "/schoolKnowledgeDetail",
                name: "schoolKnowledgeDetail",
                meta: {
                    title: "知识库详情",
                    permiss: "schoolKnowledgeDetail",
                    hidden: true
                },
                component: () => import("../views/common/schoolKnowledgeDetail.vue")
            }
        ],
    },
    {
        path: "/login",
        meta: {
            title: "登录",
            noAuth: true,
        },
        component: () => import("../views/pages/login.vue"),
    },
    {
        path: "/register",
        meta: {
            title: "注册",
            noAuth: true,
        },
        component: () => import("../views/pages/register.vue"),
    },
    {
        path: "/reset-pwd",
        meta: {
            title: "重置密码",
            noAuth: true,
        },
        component: () => import("../views/pages/reset-pwd.vue"),
    },
    {
        path: "/403",
        meta: {
            title: "没有权限",
            noAuth: true,
        },
        component: () => import("../views/pages/403.vue"),
    },
    {
        path: "/404",
        meta: {
            title: "找不到页面",
            noAuth: true,
        },
        component: () => import("../views/pages/404.vue"),
    },
    {path: "/:path(.*)", redirect: "/404"},
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    NProgress.start();
    next()
    /*const role = localStorage.getItem("vuems_name");
    const permiss = usePermissStore();
    if (!role && to.meta.noAuth !== true) {
        next("/login");
    } else if (
        typeof to.meta.permiss == "string" &&
        !permiss.key.includes(to.meta.permiss)
    ) {
        // 如果没有权限，则进入403
        next("/403");
    } else {
        next();
    }*/
});

router.afterEach(() => {
    NProgress.done();
});

export default router;
